﻿using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

using ent;

public partial class controlpanel_contents_crud : System.Web.UI.Page {
    public String URL = ApplicationCommon.getPageThis();

    public String TITLE = String.Empty;
    public String CONTENT_TYPE = String.Empty;
    public String CONTENT_PARENT_ID = String.Empty;

    private String _qsMode = String.Empty;
    private String _qsReturn = String.Empty;
    private int _qsId = 0;
    private int _qsParentId = -1;
    private int _qsType = 0;

    private TableUsers t1 = new TableUsers();
    private TableContents t2 = new TableContents();
    private TableLists t3 = new TableLists();

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        this.Page.Header.DataBind();

        // Init controls.
        ucContent.prop_ddlType.AutoPostBack = true;
        ucContent.prop_ddlType.SelectedIndexChanged += new EventHandler(ddlType_SelectedIndexChanged);
    }
    protected void Page_Load(Object sender, EventArgs e) {
        _qsMode = Request.QueryString["mode"];
        _qsReturn = Request.QueryString["return"];
        _qsId = System.Convert.ToInt32(Request.QueryString["id"]);
        if(!ApplicationCommon.isEmpty(Request.QueryString["parentid"])) {_qsParentId = System.Convert.ToInt32(Request.QueryString["parentid"]);}
        _qsType = System.Convert.ToInt32(Request.QueryString["type"]);
        getRequestJavaScript();

        // Mode.
        if(String.Equals(_qsMode, "add")) {
            viewAdd();
        } else if(String.Equals(_qsMode, "edit") && _qsId > 0) {
            viewEdit();
        } else if(String.Equals(_qsMode, "trash")) {
            viewTrash();
        } else {
            viewCrud();
        }
        // Type.
        switch(_qsType) {
            case (int)TableLists.enumContentType.Page:
                TITLE = "Page";
                CONTENT_TYPE = ((int)TableLists.enumContentType.Page).ToString();
                break;
            case (int)TableLists.enumContentType.Blog:
                TITLE = "Blog";
                CONTENT_TYPE = ((int)TableLists.enumContentType.Blog).ToString();
                break;
            case (int)TableLists.enumContentType.Post:
                TITLE = "Post";
                CONTENT_TYPE = ((int)TableLists.enumContentType.Post).ToString();
                break;
            case (int)TableLists.enumContentType.Template:
                TITLE = "Template";
                CONTENT_TYPE = ((int)TableLists.enumContentType.Template).ToString();
                break;
            case (int)TableLists.enumContentType.Style:
                TITLE = "Style";
                CONTENT_TYPE = ((int)TableLists.enumContentType.Style).ToString();
                break;
            case (int)TableLists.enumContentType.System:
                TITLE = "System Script";
                CONTENT_TYPE = ((int)TableLists.enumContentType.System).ToString();
                break;
            default:
                break;
        }
        CONTENT_PARENT_ID = _qsParentId.ToString();
        Page.Title = TITLE + " Management";
    }

#region Methods

    /// <summary>Get HTTP request from JavaScript postback.</summary>
    protected void getRequestJavaScript() {
        if(!Page.IsPostBack) {return;}

        String eventName = Request.Form["__EVENTTARGET"];
        String eventArgument = Request.Form["__EVENTARGUMENT"];

        if(String.Equals(eventName, "onMode")) {
            // Get multiple values from serialized string.
            System.Collections.Specialized.NameValueCollection aryEventArguments = System.Web.HttpUtility.ParseQueryString(eventArgument);
            // Set mode by query string.
            if(String.Equals(aryEventArguments["action"], "add")) {
                Hashtable qs1 = new Hashtable();
                qs1.Add("mode", "add");
                qs1.Add("type", _qsType);
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            } else if(String.Equals(aryEventArguments["action"], "edit")) {
                if(!ApplicationCommon.isEmpty(aryEventArguments["id"])) {
                    Hashtable qs1 = new Hashtable();
                    qs1.Add("mode", "edit");
                    qs1.Add("type", _qsType);
                    qs1.Add("id", System.Convert.ToInt32(aryEventArguments["id"]));
                    Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
                }
            } else if(String.Equals(aryEventArguments["action"], "trash")) {
                Hashtable qs1 = new Hashtable();
                qs1.Add("mode", "trash");
                qs1.Add("type", _qsType);
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            } else {
                Hashtable qs1 = new Hashtable();
                qs1.Add("type", _qsType);
                Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
            }
        }
    }
    /// <summary>Init form input values.</summary>
    protected void initFormInputValues() {
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            // Get list: content type.
            ucContent.prop_ddlType.DataSource = t3.getList((int)TableLists.enumList.ContentTypes, null);
            ucContent.prop_ddlType.DataTextField = TableLists.TBL__lists__list_name;
            ucContent.prop_ddlType.DataValueField = TableLists.TBL__lists__list_value;
            ucContent.prop_ddlType.DataBind();
            // Get list: content parent.
            switch(_qsType) {
                case (int)TableLists.enumContentType.Post:
                    ucContent.prop_ddlParent.DataSource = t2.getContentsByTypeForList(TableLists.enumContentType.Blog, _qsId);
                    break;
                default:
                    ucContent.prop_ddlParent.DataSource = t2.getContentsByTypeForList(TableLists.enumContentType.Page, _qsId);
                    break;
            }
            ucContent.prop_ddlParent.DataTextField = TableContents.TBL__contents__content_title;
            ucContent.prop_ddlParent.DataValueField = TableContents.TBL__contents__content_id;
            ucContent.prop_ddlParent.DataBind();
            ucContent.prop_ddlParent.Items.Insert(0, new ListItem("Root", "0"));
            // Get list: assigned to.
            DataTable dtAssignedTo = t1.getUsersBrief();
            foreach(DataRow row in dtAssignedTo.Rows) {
                ucContent.prop_ddlAssignedTo.Items.Add(new ListItem(row[TableUsers.TBL__users__user_name_first].ToString() + " " + row[TableUsers.TBL__users__user_name_last].ToString(), row[TableUsers.TBL__users__user_id].ToString()));
            }
            // Get list: template type.
            ucContent.prop_ddlTemplate.DataSource = t2.getContentsByTypeForList(TableLists.enumContentType.Template, _qsId);
            ucContent.prop_ddlTemplate.DataTextField = TableContents.TBL__contents__content_title;
            ucContent.prop_ddlTemplate.DataValueField = TableContents.TBL__contents__content_id;
            ucContent.prop_ddlTemplate.DataBind();
            ucContent.prop_ddlTemplate.Items.Insert(0, new ListItem("None", "0"));
            // Get list: style type.
            ucContent.prop_ddlStyle.DataSource = t2.getContentsByTypeForList(TableLists.enumContentType.Style, _qsId);
            ucContent.prop_ddlStyle.DataTextField = TableContents.TBL__contents__content_title;
            ucContent.prop_ddlStyle.DataValueField = TableContents.TBL__contents__content_id;
            ucContent.prop_ddlStyle.DataBind();
            ucContent.prop_ddlStyle.Items.Insert(0, new ListItem("None", "0"));
            // Get list: status.
            ucContent.prop_ddlStatus.DataSource = t3.getList((int)TableLists.enumList.ContentStatuses, null);
            ucContent.prop_ddlStatus.DataTextField = TableLists.TBL__lists__list_name;
            ucContent.prop_ddlStatus.DataValueField = TableLists.TBL__lists__list_value;
            ucContent.prop_ddlStatus.DataBind();
            switch(_qsType) {
                case (int)TableLists.enumContentType.Blog:
                case (int)TableLists.enumContentType.Template:
                case (int)TableLists.enumContentType.Style:
                case (int)TableLists.enumContentType.System:
                    ucContent.prop_ddlStatus.Items.FindByValue(((int)TableLists.enumContentStatus.Publish).ToString()).Selected = true;
                    break;
            }
            // Get list: order type.
            ucContent.prop_ddlOrderType.DataSource = t3.getList((int)TableLists.enumList.ContentOrderTypes, null);
            ucContent.prop_ddlOrderType.DataTextField = TableLists.TBL__lists__list_name;
            ucContent.prop_ddlOrderType.DataValueField = TableLists.TBL__lists__list_value;
            ucContent.prop_ddlOrderType.DataBind();
        }
        // Change form base on type.
        switch(_qsType) {
            case (int)TableLists.enumContentType.Page:
                plhJavaScriptTextEditor.Visible = true;
                ucContent.prop_rowParent.Visible = true;
                ucContent.prop_rowAssignedTo.Visible = true;
                ucContent.prop_pnlHeader.Visible = false;
                ucContent.prop_pnlFooter.Visible = false;
                ucContent.prop_rowStatus.Visible = true;
                ucContent.prop_rowTemplate.Visible = false;
                ucContent.prop_rowStyle.Visible = true;
                ucContent.prop_rowOrder.Visible = false;
                ucContent.prop_rowOrderType.Visible = false;
                ucContent.prop_rowMenuOrder.Visible = true;
                break;
            case (int)TableLists.enumContentType.Blog:
                plhJavaScriptTextEditor.Visible = false;
                ucContent.prop_rowParent.Visible = true;
                ucContent.prop_rowAssignedTo.Visible = true;
                ucContent.prop_pnlHeader.Visible = false;
                ucContent.prop_pnlBody.Visible = false;
                ucContent.prop_pnlFooter.Visible = false;
                ucContent.prop_rowStatus.Visible = false;
                ucContent.prop_rowTemplate.Visible = false;
                ucContent.prop_rowStyle.Visible = true;
                ucContent.prop_rowOrder.Visible = false;
                ucContent.prop_rowOrderType.Visible = true;
                ucContent.prop_rowMenuOrder.Visible = true;
                break;
            case (int)TableLists.enumContentType.Post:
                plhJavaScriptTextEditor.Visible = true;
                ucContent.prop_rowParent.Visible = true;
                ucContent.prop_rowAssignedTo.Visible = false;
                ucContent.prop_pnlHeader.Visible = false;
                ucContent.prop_pnlFooter.Visible = false;
                ucContent.prop_rowStatus.Visible = true;
                ucContent.prop_rowTemplate.Visible = false;
                ucContent.prop_rowStyle.Visible = false;
                ucContent.prop_rowOrder.Visible = true;
                ucContent.prop_rowOrderType.Visible = false;
                ucContent.prop_rowMenuOrder.Visible = false;
                break;
            case (int)TableLists.enumContentType.Template:
                plhJavaScriptTextEditor.Visible = false;
                plhJavaScriptFormSpecial.Visible = true;
                ucContent.prop_rowParent.Visible = false;
                ucContent.prop_rowAssignedTo.Visible = false;
                ucContent.prop_rowParent.Visible = false;
                ucContent.prop_pnlHeader.Visible = true;
                ucContent.prop_pnlFooter.Visible = false;
                ucContent.prop_pnlMeta.Visible = false;
                break;
            case (int)TableLists.enumContentType.Style:
                plhJavaScriptTextEditor.Visible = false;
                plhJavaScriptFormSpecial.Visible = true;
                ucContent.prop_rowParent.Visible = false;
                ucContent.prop_rowAssignedTo.Visible = false;
                ucContent.prop_rowParent.Visible = false;
                ucContent.prop_pnlHeader.Visible = true;
                ucContent.prop_pnlFooter.Visible = false;
                ucContent.prop_pnlMeta.Visible = false;
                break;
            case (int)TableLists.enumContentType.System:
                plhJavaScriptTextEditor.Visible = false;
                plhJavaScriptFormSpecial.Visible = true;
                ucContent.prop_rowParent.Visible = false;
                ucContent.prop_rowAssignedTo.Visible = false;
                ucContent.prop_pnlMeta.Visible = false;
                break;
            default:
                break;
        }
    }
    /// <summary>View in CRUD mode.</summary>
    protected void viewCrud() {
        Master.prop_plhHeadYui.Visible = true;
        plhClientScript.Visible = true;
        plhJavaScriptDataTableCrud.Visible = true;
        plhCrud.Visible = true;
    }
    /// <summary>View in add mode.</summary>
    protected void viewAdd() {
        plhAdd.Visible = true;
        plhForm.Visible = true;
        // Init form input values.
        initFormInputValues();
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            switch(_qsType) {
                case (int)TableLists.enumContentType.Page:
                case (int)TableLists.enumContentType.Blog:
                    ucContent.prop_ddlAssignedTo.Items.FindByValue(TableLists.getAssignedToByRole(Session).ToString()).Selected = true;
                    break;
            }
            ucContent.prop_ddlStatus.Items.FindByValue(((int)TableLists.enumContentStatus.Draft).ToString()).Selected = true;
        }
    }
    /// <summary>View in edit mode.</summary>
    protected void viewEdit() {
        plhEdit.Visible = true;
        plhForm.Visible = true;
        DataRow dr1 = t2.getContent(_qsId);
        if(_qsType <= 0) {
            _qsType = (int)dr1[TableContents.TBL__contents__content_type];
        }
        // Init form input values.
        initFormInputValues();
        // First load data for manipulation before postback.
        if(!Page.IsPostBack) {
            ucContent.prop_ddlType.Items.FindByValue(dr1[TableContents.TBL__contents__content_type].ToString()).Selected = true;
            ucContent.prop_ddlParent.Items.FindByValue(dr1[TableContents.TBL__contents__content_parent_id].ToString()).Selected = true;
            ucContent.prop_ddlAssignedTo.Items.FindByValue(dr1[TableContents.TBL__contents__content_assignedto].ToString()).Selected = true;
            ucContent.prop_txtTitle.Text = (String)dr1[TableContents.TBL__contents__content_title];
            ucContent.prop_txtHeader.Text = (String)dr1[TableContents.TBL__contents__content_header];
            if(_qsType == (int)TableLists.enumContentType.Page || _qsType == (int)TableLists.enumContentType.Post) {
                ucContent.prop_txtBody.Text = (String)dr1[TableContents.TBL__contents__content_body];
            } else {
                ucContent.prop_txtBody.Text = HttpUtility.HtmlDecode((String)dr1[TableContents.TBL__contents__content_body]);
            }
            ucContent.prop_txtFooter.Text = (String)dr1[TableContents.TBL__contents__content_footer];
            ucContent.prop_ddlStatus.Items.FindByValue(dr1[TableContents.TBL__contents__content_status].ToString()).Selected = true;
            ucContent.prop_ddlTemplate.Items.FindByValue(dr1[TableContents.TBL__contents__content_template_id].ToString()).Selected = true;
            ucContent.prop_ddlStyle.Items.FindByValue(dr1[TableContents.TBL__contents__content_style_id].ToString()).Selected = true;
            ucContent.prop_txtOrder.Text = (String)dr1[TableContents.TBL__contents__content_order].ToString();
            ucContent.prop_ddlOrderType.Items.FindByValue(dr1[TableContents.TBL__contents__content_order_type].ToString()).Selected = true;
            ucContent.prop_txtMenuOrder.Text = dr1[TableContents.TBL__contents__content_order_menu].ToString();
        }
    }
    /// <summary>View in trash mode.</summary>
    protected void viewTrash() {
        Master.prop_plhHeadYui.Visible = true;
        plhClientScript.Visible = true;
        plhJavaScriptDataTableEdit.Visible = true;
        plhTrash.Visible = true;
    }
    /// <summary>Return user from (previous page).</summary>
    protected bool returnUserFrom() {
        String _qsReturn = Request.QueryString["return"];
        if(!ApplicationCommon.isEmpty(_qsReturn)) {
            Response.Redirect(HttpUtility.UrlDecode(_qsReturn));
        }
        return false;
    }

#endregion

#region Events

    protected void btnFormOkay_Click(Object sender, EventArgs e) {
        if(plhAdd.Visible) {
            Hashtable p1 = new Hashtable();
            p1.Add(TableContents.TBL__contents__content_author_user_id, (int)Session[ApplicationCommon.SESSION__user_id]);
            p1.Add(TableContents.TBL__contents__content_editor_user_id, (int)Session[ApplicationCommon.SESSION__user_id]);
            p1.Add(TableContents.TBL__contents__content_type, ucContent.prop_ddlType.SelectedValue);
            p1.Add(TableContents.TBL__contents__content_parent_id, ucContent.prop_ddlParent.SelectedValue);
            p1.Add(TableContents.TBL__contents__content_assignedto, ucContent.prop_ddlAssignedTo.SelectedValue);
            p1.Add(TableContents.TBL__contents__content_title, DatabaseCommon.trimAll(ucContent.prop_txtTitle.Text));
            p1.Add(TableContents.TBL__contents__content_title_slug, TableContents.toSlug(ucContent.prop_txtTitle.Text));
            p1.Add(TableContents.TBL__contents__content_header, ucContent.prop_txtHeader.Text);
            p1.Add(TableContents.TBL__contents__content_body, ucContent.prop_txtBody.Text);
            p1.Add(TableContents.TBL__contents__content_footer, ucContent.prop_txtFooter.Text);
            p1.Add(TableContents.TBL__contents__content_status, ucContent.prop_ddlStatus.SelectedValue);
            p1.Add(TableContents.TBL__contents__content_template_id, ucContent.prop_ddlTemplate.SelectedValue);
            p1.Add(TableContents.TBL__contents__content_style_id, ucContent.prop_ddlStyle.SelectedValue);
            p1.Add(TableContents.TBL__contents__content_order, ucContent.prop_txtOrder.Text);
            p1.Add(TableContents.TBL__contents__content_order_type, ucContent.prop_ddlOrderType.SelectedValue);
            p1.Add(TableContents.TBL__contents__content_order_menu, ucContent.prop_txtMenuOrder.Text);
            if(System.Convert.ToInt32(ucContent.prop_ddlStatus.SelectedValue) == (int)TableLists.enumContentStatus.Publish) {
                p1.Add(TableContents.TBL__contents__content_date_published, DateTime.Now);
            }
            p1.Add(TableContents.TBL__contents__content_date_modified, DateTime.Now);
            p1.Add(TableContents.TBL__contents__content_date_created, DateTime.Now);
            t2.createContent(p1);
        } else if(plhEdit.Visible) {
            Hashtable p2 = new Hashtable();
            p2.Add(TableContents.TBL__contents__content_id, _qsId);
            p2.Add(TableContents.TBL__contents__content_editor_user_id, (int)Session[ApplicationCommon.SESSION__user_id]);
            p2.Add(TableContents.TBL__contents__content_type, ucContent.prop_ddlType.SelectedValue);
            p2.Add(TableContents.TBL__contents__content_parent_id, ucContent.prop_ddlParent.SelectedValue);
            p2.Add(TableContents.TBL__contents__content_assignedto, ucContent.prop_ddlAssignedTo.SelectedValue);
            p2.Add(TableContents.TBL__contents__content_title, DatabaseCommon.trimAll(ucContent.prop_txtTitle.Text));
            p2.Add(TableContents.TBL__contents__content_title_slug, TableContents.toSlug(ucContent.prop_txtTitle.Text));
            p2.Add(TableContents.TBL__contents__content_header, ucContent.prop_txtHeader.Text);
            p2.Add(TableContents.TBL__contents__content_body, ucContent.prop_txtBody.Text);
            p2.Add(TableContents.TBL__contents__content_footer, ucContent.prop_txtFooter.Text);
            p2.Add(TableContents.TBL__contents__content_status, ucContent.prop_ddlStatus.SelectedValue);
            p2.Add(TableContents.TBL__contents__content_template_id, ucContent.prop_ddlTemplate.SelectedValue);
            p2.Add(TableContents.TBL__contents__content_style_id, ucContent.prop_ddlStyle.SelectedValue);
            p2.Add(TableContents.TBL__contents__content_order, ucContent.prop_txtOrder.Text);
            p2.Add(TableContents.TBL__contents__content_order_type, ucContent.prop_ddlOrderType.SelectedValue);
            p2.Add(TableContents.TBL__contents__content_order_menu, ucContent.prop_txtMenuOrder.Text);
            if(System.Convert.ToInt32(ucContent.prop_ddlStatus.SelectedValue) == (int)TableLists.enumContentStatus.Publish) {
                p2.Add(TableContents.TBL__contents__content_date_published, DateTime.Now);
            }
            p2.Add(TableContents.TBL__contents__content_date_modified, DateTime.Now);
            t2.setContent(p2);
        }
        if(!returnUserFrom()) {
            Hashtable qs1 = new Hashtable();
            qs1.Add("type", _qsType);
            Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
        }
    }
    protected void btnFormCancel_Click(Object sender, EventArgs e) {
        if(!returnUserFrom()) {
            Hashtable qs1 = new Hashtable();
            qs1.Add("type", _qsType);
            Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
        }
    }
    protected void ddlType_SelectedIndexChanged(Object sender, EventArgs e) {
        Hashtable qs1 = new Hashtable();
        qs1.Add("mode", _qsMode);
        if(_qsId > 0) {qs1.Add("id", _qsId);}
        if(!ApplicationCommon.isEmpty(ucContent.prop_ddlType.SelectedValue)) {
            qs1.Add("type", System.Convert.ToInt32(ucContent.prop_ddlType.SelectedValue));
        }
        Response.Redirect(ApplicationCommon.setQueryString(qs1, URL));
    }

#endregion

    /// <summary>PreRender is called by the page life cycle after the events are called (e.g. click events).</summary>
    /// <remarks>http://blogs.thesitedoctor.co.uk/tim/2006/06/30/Complete+Lifecycle+Of+An+ASPNet+Page+And+Controls.aspx</remarks>
    protected void Page_PreRender(Object sender, EventArgs e) {
        if(_qsType > 0 && ucContent.Visible) {
            ucContent.prop_ddlType.ClearSelection();
            ucContent.prop_ddlType.Items.FindByValue(_qsType.ToString()).Selected = true;
        }
    }

}